package cn.itcast.view;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import cn.itcast.pojo.User;

public class UserExcelView extends AbstractXlsView {
	/**
	 * model：就是exportExcel方法中封装了数据model对象 workbook：poi中的API，用来创建excel
	 * 
	 */
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 获取model中封装的数据
		List<User> userList = (List<User>) model.get("userList");
		// 创建sheet
		Sheet sheet = workbook.createSheet("会员列表");
		// 创建表的标题行
		Row header = sheet.createRow(0);
		// 在第一行创建单元格
		header.createCell(0).setCellValue("ID");
		header.createCell(1).setCellValue("用户名");
		header.createCell(2).setCellValue("姓名");
		header.createCell(3).setCellValue("年龄");
		header.createCell(4).setCellValue("性别");
		header.createCell(5).setCellValue("出生日期");
		header.createCell(6).setCellValue("创建日期");
		header.createCell(7).setCellValue("更新日期");
		int count = 1;
		for (User user : userList) {
			// 创建表的标题下的第一行
			Row row = sheet.createRow(count);
			row.createCell(0).setCellValue(user.getId());
			row.createCell(1).setCellValue(user.getUserName());
			row.createCell(2).setCellValue(user.getName());
			row.createCell(3).setCellValue(user.getAge());
			String sex = null;
			if (user.getSex() == 1) {
				sex = "男";
			} else if (user.getSex() == 2) {
				sex = "女";
			}
			row.createCell(4).setCellValue(sex);
			row.createCell(5).setCellValue(new DateTime(user.getBirthday()).toString("yyyy-MM-dd"));
			row.createCell(6).setCellValue(new DateTime(user.getCreated()).toString("yyyy-MM-dd hh-mm-ss"));
			row.createCell(7).setCellValue(new DateTime(user.getUpdated()).toString("yyyy-MM-dd hh-mm-ss"));
			count++;
		}
		// 设置相应头信息，以附件形式下载并且指定文件名
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String("会员列表.xls".getBytes(), "ISO-8859-1"));
	}

}
